package com.zbbt.sportslottery.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.zbbt.sportslottery.app.App;

public class FileUtil {

	public static synchronized void saveObject2File(Serializable serializableObject, String fileName) {
		try {
			File file = new File(App.OBJECT_CACHE_PATH, fileName);
			if (null == serializableObject) {
				if (file != null && file.exists()) {
					file.delete();
				}
				return;
			}
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(serializableObject);
			oos.close();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static synchronized Serializable readObjectFromFile(String fileName) {
		Serializable object = null;
		try {
			File file = new File(App.OBJECT_CACHE_PATH, fileName);
			if (file.exists()) {
				System.out.println(App.OBJECT_CACHE_PATH);
				FileInputStream fis = new FileInputStream(file);
				ObjectInputStream ois = new ObjectInputStream(fis);
				object = (Serializable) ois.readObject();
				ois.close();
				fis.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return object;
	}

}